package it.polimi.meteocal_pappalardo_sacchi.bean;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 * Created by Alessandro on 21/01/15.
 */
@FacesConverter("notificationSnippetConverter")
public class NotificationSnippetConverter implements Converter {

    private static final int ENDING_POINT = 75;

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) throws ConverterException {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) throws ConverterException {
        if (!(o instanceof String)) {
            throw new ConverterException("Not a string");
        }

        String notificationMessage = (String) o;

        if (notificationMessage.length() > ENDING_POINT) {
            String sample = notificationMessage.substring(0, ENDING_POINT);
            sample += "...";
            return sample;
        } else {
            return notificationMessage;
        }
    }
}
